package com.dinzeer.mtrmod.Attribute;
import net.minecraftforge.registries.RegistryObject;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.event.lifecycle.FMLConstructModEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.EntityAttributeModificationEvent;

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.ai.attributes.RangedAttribute;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.Mob;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.Entity;
import com.dinzeer.mtrmod.MTRMOD;

import java.util.List;
import java.util.Objects;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)

public class attr {
    public static final DeferredRegister<Attribute> ATTRIBUTES = DeferredRegister.create(ForgeRegistries.ATTRIBUTES, MTRMOD.MODID);
    public static final RegistryObject<Attribute> strength = ATTRIBUTES.register("strength", () -> (new RangedAttribute("attribute." + MTRMOD.MODID + ".strength", 0, 0, 100000000)).setSyncable(true));
    public static final RegistryObject<Attribute> agility = ATTRIBUTES.register("agility", () -> (new RangedAttribute("attribute." + MTRMOD.MODID + ".agility", 0, 0, 100000000)).setSyncable(true));
    public static final RegistryObject<Attribute> magic = ATTRIBUTES.register("magic", () -> (new RangedAttribute("attribute." + MTRMOD.MODID + ".magic", 0, 0, 100000000)).setSyncable(true));

    @SubscribeEvent
    public static void register(FMLConstructModEvent event) {
        event.enqueueWork(() -> ATTRIBUTES.register(FMLJavaModLoadingContext.get().getModEventBus()));
    }

    @SubscribeEvent
    public static void addAttributes(EntityAttributeModificationEvent event) {
        List<EntityType<? extends LivingEntity>> entityTypes = event.getTypes();
        entityTypes.forEach((e) -> {
            Class<? extends Entity> baseClass = e.getBaseClass();
            if (baseClass.isAssignableFrom(Mob.class)) {
                event.add(e, strength.get());
            }
        });
        entityTypes.forEach((e) -> {
            Class<? extends Entity> baseClass = e.getBaseClass();
            if (baseClass.isAssignableFrom(Mob.class)) {
                event.add(e, agility.get());
            }
        });
        entityTypes.forEach((e) -> {
            Class<? extends Entity> baseClass = e.getBaseClass();
            if (baseClass.isAssignableFrom(Mob.class)) {
                event.add(e, magic.get());
            }
        });


    }

    @Mod.EventBusSubscriber
    private class Utils {
        @SubscribeEvent
        public static void persistAttributes(PlayerEvent.Clone event) {
            Player oldP = event.getOriginal();
            Player newP = event.getEntity();
            Objects.requireNonNull(newP.getAttribute(strength.get())).setBaseValue(Objects.requireNonNull(oldP.getAttribute(strength.get())).getBaseValue());
            Objects.requireNonNull(newP.getAttribute(agility.get())).setBaseValue(Objects.requireNonNull(oldP.getAttribute(agility.get())).getBaseValue());
            Objects.requireNonNull(newP.getAttribute(magic.get())).setBaseValue(Objects.requireNonNull(oldP.getAttribute(magic.get())).getBaseValue());


        }
    }
}

